package meijuli.com.join51.business.ui;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.utils.DensityUtil;


/**
 * @author Scott Smith  @Date 2016年04月16/4/22日 15:27
 */
public class DefaultFooterView extends FrameLayout implements LoadMoreUIHandler {
    private ProgressBar mProgressBar;
    private TextView mTxtContent;

    public DefaultFooterView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        LinearLayout ll = new LinearLayout(context);
        LayoutParams lp0 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp0.gravity = Gravity.CENTER;
        ll.setPadding(0, DensityUtil.dip2px(10), 0, DensityUtil.dip2px(10));
        ll.setLayoutParams(lp0);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.gravity = Gravity.CENTER;

        mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.ptr_progressbar, ll, false);
        mProgressBar.setLayoutParams(lp1);
        mProgressBar.setPadding(DensityUtil.dip2px(5), 0, DensityUtil.dip2px(5), 0);
        ll.addView(mProgressBar);

        mTxtContent = new TextView(context);
        mTxtContent.setTextColor(getResources().getColor(R.color.black_normal));
        mTxtContent.setTextSize(16.0f);
        mTxtContent.setLayoutParams(lp1);
        ll.addView(mTxtContent);

        addView(ll);
    }


    public void setText(CharSequence text) {
        mTxtContent.setText(text);
    }

    @Override
    public void onLoading(LoadMoreViewBase container) {
        mTxtContent.setText("加载中…");
        if (VISIBLE != mProgressBar.getVisibility()) mProgressBar.setVisibility(VISIBLE);
        if (VISIBLE != mTxtContent.getVisibility()) mTxtContent.setVisibility(VISIBLE);
    }

    @Override
    public void onLoadFinish(LoadMoreViewBase container, boolean empty, boolean hasMore) {
        mProgressBar.setVisibility(GONE);
        mTxtContent.setVisibility(GONE);
    }

    @Override
    public void onWaitToLoadMore(LoadMoreViewBase container) {

    }

    @Override
    public void onLoadError(LoadMoreViewBase container, int errorCode, String errorMessage) {
        mProgressBar.setVisibility(GONE);
        mTxtContent.setText(errorMessage);
        mTxtContent.setVisibility(VISIBLE);
    }

    /**
     * 加载完成
     *
     * @param isLast  是否是最后一页
     * @param message 提示信息
     */
    public void onLoadComplete(boolean isLast, CharSequence message) {
        if (isLast) {
            if (!TextUtils.isEmpty(message)) {
                mTxtContent.setText(message);
            } else {
                mTxtContent.setText(R.string.ptr_no_more);
            }

            mProgressBar.setVisibility(GONE);
            mTxtContent.setVisibility(VISIBLE);
        } else {
            onLoadFinish(null, false, false);
        }
    }
}
